//
//  TMCutterSelector.m
//  cutter
//
//  Created by mac on 15/12/11.
//  Copyright © 2015年 e360. All rights reserved.
//
#import "TMCutterLayer.h"
#import "TMCutterSelector.h"

@implementation TMCutterSelector
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
        [self setUserInteractionEnabled:NO];
    }
    return self;
}
-(void)drawRect:(CGRect)rect{
    if (self.target) {
        [self updateController];
        CGContextRef context=UIGraphicsGetCurrentContext();
        [self drawController:context];
        [self drawPoint:context anchor:lt];
        [self drawPoint:context anchor:rt];
        [self drawPoint:context anchor:rb];
        [self drawPoint:context anchor:lb];
        [self drawPoint:context anchor:ca];
    }
}
-(void)drawController:(CGContextRef)context{
    CGContextSaveGState(context);
    CGContextSetShouldAntialias(context,true);
    //
    CGContextSetLineWidth(context, 4);
    CGContextSetStrokeColorWithColor(context, [[UIColor darkGrayColor] CGColor]);
    CGContextMoveToPoint(context, lt.x, lt.y);
    CGContextAddLineToPoint(context, rt.x, rt.y);
    CGContextAddLineToPoint(context, rb.x, rb.y);
    CGContextAddLineToPoint(context, lb.x, lb.y);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathStroke);
    CGContextMoveToPoint(context, ca.x, ca.y);
    CGContextAddLineToPoint(context, cb.x, cb.y);
    CGContextDrawPath(context, kCGPathStroke);
    //
    CGContextRestoreGState(context);
}
-(void)drawPoint:(CGContextRef)context anchor:(CGPoint)anchor{
    CGContextSaveGState(context);
    CGContextSetShouldAntialias(context,false);
    CGContextTranslateCTM(context, anchor.x, anchor.y);
    CGContextRotateCTM(context,self.target.rotationZ);
    //
    CGContextSetLineWidth(context, 1);
    CGContextSetStrokeColorWithColor(context, [[UIColor darkGrayColor] CGColor]);
    CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
    CGContextAddRect(context, CGRectMake(-6, -6, 12, 12));
    CGContextDrawPath(context, kCGPathFillStroke);
    //
    CGContextRestoreGState(context);
}
//
-(void)updateController{
    if (self.target) {
        CGRect  bounds = CGRectInset(self.target.bounds, -4, -4);
        CGFloat topy = CGRectGetMinY(bounds) - 10;
        CGFloat maxy = CGRectGetMaxY(bounds);
        CGFloat maxx = CGRectGetMaxX(bounds);
        CGFloat miny = CGRectGetMinY(bounds);
        CGFloat minx = CGRectGetMinX(bounds);
        CGFloat midx = CGRectGetMidX(bounds);
        CGFloat midy = CGRectGetMidY(bounds);
        //
        oo = [self.target convertPoint:CGPointMake(midx, midy) toView:self];
        lt = [self.target convertPoint:CGPointMake(minx, miny) toView:self];
        rt = [self.target convertPoint:CGPointMake(maxx, miny) toView:self];
        rb = [self.target convertPoint:CGPointMake(maxx, maxy) toView:self];
        lb = [self.target convertPoint:CGPointMake(minx, maxy) toView:self];
        cb = [self.target convertPoint:CGPointMake(midx, miny) toView:self];
        ca = [self.target convertPoint:CGPointMake(midx, topy) toView:self];
        //
        CGFloat u = 80 / [self getDistance:ca b:cb];
        ca = CGPointMake(cb.x + u * (ca.x - cb.x), cb.y + u * (ca.y - cb.y));
    }
}
//
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if (self.target != nil) {
        CGFloat pe = 20;
        CGPoint cp = [touch locationInView:self];
        if ([self getDistance:cp b:ca] < pe) {
            type = 3;
            return;
        }
        if ([self getDistance:cp b:lt] < pe ||
            [self getDistance:cp b:rt] < pe ||
            [self getDistance:cp b:rb] < pe ||
            [self getDistance:cp b:lb] < pe) {
            type = 2;
            return;
        }
    }
    //
    TMCutterLayer *view = (id)[touch view];
    if ([view isKindOfClass:TMCutterLayer.class]){
        type = 1;
        [self setTarget:view];
    }else{
        type = 0;
        [self setTarget:nil];
    }
    [self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.target && self.target.enabled) {
        UITouch *touch = [touches anyObject];
        CGPoint curPoint = [touch locationInView:self];
        CGPoint prePoint = [touch previousLocationInView:self];
        if (type == 1) {
            curPoint = [self convertPoint:curPoint toView:self.target.superview];
            prePoint = [self convertPoint:prePoint toView:self.target.superview];
            self.target.x += curPoint.x - prePoint.x;
            self.target.y += curPoint.y - prePoint.y;
        }
        //
        if (type == 2) {
            CGFloat ts = [self getDistance:curPoint b:oo] / [self getDistance:prePoint b:oo];
            self.target.scaleX *= ts;
            self.target.scaleY *= ts;
        }
        //
        if (type == 3) {
            CGFloat ta = [self getAngle:curPoint b:oo] - [self getAngle:prePoint b:oo];
            self.target.rotationZ += ta;
        }
        [self.target setNeedsLayout];
        [self setNeedsDisplay];
    }
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (self.target && self.target.enabled) {
        UITouch *touch = [touches anyObject];
        if (touch.tapCount == 2) {
            [self.target setRotationZ:0];
            [self.target setNeedsLayout];
        }
        [self setNeedsDisplay];
    }
}
//
-(CGFloat)getDistance:(CGPoint)a b:(CGPoint)b{
    CGFloat dx = a.x - b.x;
    CGFloat dy = a.y - b.y;
    return sqrt(dx * dx + dy * dy);
}
-(CGFloat)getAngle:(CGPoint)a b:(CGPoint)b{
    CGFloat dx = a.x - b.x;
    CGFloat dy = a.y - b.y;
    return atan2(dy,dx);
}
@end
